import re

from flask import request
from frame.util.PlaywrightUtil import PlaywrightUtil  # 导入计算逻辑函数
from frame.exception.ApiException import ApiException
import asyncio
from frame.util.Crawl4aiUtil import Crawl4aiUtil
from frame.library.crawl4ai import *


class Index:
    # 获取html内容
    def get_html_conntent(self, url, wait_time):
        # content = get_web_text('https://www.xsframe.cn')

        target_url = request.args.get('url')
        wait_time = request.args.get('time')
        user_agent = request.args.get('user_agent')

        # app.logger.error('This is an error message')

        if not target_url:
            raise ApiException(msg='URL is required')

        if "mp.weixin.qq.com" in url:
            return "爬取失败"

        # 以下方式可能存在一个问题就是不能跳过爬虫 TODO
        # crawler = PlaywrightUtil()
        # content = crawler.scrape(target_url, wait_time)

        # 下方方式可以跳过爬虫，但是返回的是markdown格式的数据 TODO 待优化
        crawl4ai = Crawl4aiUtil()
        # content = asyncio.run(crawl4ai.get_text_content(target_url))
        content = asyncio.run(crawl4ai.get_markdown_content(target_url, user_agent))

        return content
